﻿using System;
using System.Drawing;
using System.Windows.Forms;
using socket_server_01;
using System.Drawing.Printing;
using ThoughtWorks.QRCode.Codec;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private string print_str = "请输入打印内容.....";
        private string qrcode = "123456";
        private static int fontsize = 8;
        private string pid_name = "水洗";
        private string expresname = "xx";

        public Form1()
        {
            InitializeComponent();
        }



        /// <summary>
        /// 加载完成后开启socket监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            Class1 ps = new Class1();
            ps.run(27778);
        }

        public  void print_last(string str,short pages = 1,string qrcode="123456",string pid_name = "水洗",string expresname = "xx")
        {
            
            Form1 obj = new Form1();

            obj.print_str = str;
            obj.qrcode = qrcode;
            obj.pid_name = pid_name;
            obj.expresname = expresname;

            obj.printDocument1.PrinterSettings.Copies = pages;
            obj.printDocument1.Print();
            //MessageBox.Show(Form1.print_str);
        }


        /// <summary>
        /// 打印测试按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
                //MessageBox.Show(Form1.print_str);
            }
        }

       /// <summary>
       /// 打印预览按钮
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog();
        }

        /// <summary>
        /// 打印设置按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //页面设置部分
            //printDocument1.OriginAtMargins = true;
            //PaperSize ps = new PaperSize("Custom Size 1", 430, 300);
            //printDocument1.DefaultPageSettings.PaperSize = ps;
            //printDocument1.DefaultPageSettings.Margins.Top = 100;
            //printDocument1.DefaultPageSettings.Margins.Bottom = 100;
            

            //生成二维码部分
            QRCodeEncoder qr = new QRCodeEncoder();
            qr.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qr.QRCodeScale = 2;
            qr.QRCodeVersion = 2;
            qr.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

            //画图画二维码部分
            Bitmap bmp = null;
            bmp = qr.Encode(this.qrcode);
            e.Graphics.DrawImage(bmp, 30, 0);

            //写文字部分----洗涤类别
            string str_pid_name = this.pid_name;
            Font font1 = new Font("黑体", 12);
            Brush bru1 = Brushes.Black;
            StringFormat fmt1 = new StringFormat();
            fmt1.LineAlignment = StringAlignment.Near;//左对齐
            fmt1.FormatFlags = StringFormatFlags.LineLimit;//自动换行
            Rectangle r1 = new Rectangle(18, 60, 80, 400);//设定文本打印区域 b是左上角坐标，Size是打印区域（矩形） float mmtopt = 2.835f; 单位换算，毫米*2.835=Point单位
            e.Graphics.DrawString(str_pid_name, font1, bru1, r1, fmt1);

            //划线
            e.Graphics.DrawLine(new Pen(Color.Black, 2), 95, 0, 95, 80);

            //写文字部分----文字主体部分
            string str = this.print_str;
            Font font = new Font("黑体", Form1.fontsize);
            Brush bru = Brushes.Black;
            StringFormat fmt = new StringFormat();
            fmt.LineAlignment = StringAlignment.Near;//左对齐
            fmt.FormatFlags = StringFormatFlags.LineLimit;//自动换行
            Rectangle r = new Rectangle(100, 0, 220, 400);//设定文本打印区域 b是左上角坐标，Size是打印区域（矩形） float mmtopt = 2.835f; 单位换算，毫米*2.835=Point单位
            e.Graphics.DrawString(str, font, bru, r, fmt);

            //划线
            e.Graphics.DrawLine(new Pen(Color.Black,2),330,0,330,70);

            

            //写文字部分---物流员
            string str_express = this.expresname;
            Font font_express = new Font("黑体", 11);
            Brush bru_express = Brushes.Black;
            StringFormat fmt_express = new StringFormat();
            fmt_express.LineAlignment = StringAlignment.Near;//左对齐
            fmt_express.FormatFlags = StringFormatFlags.LineLimit;//自动换行
            Rectangle r_express = new Rectangle(340, 0, 75, 400);//设定文本打印区域 b是左上角坐标，Size是打印区域（矩形） float mmtopt = 2.835f; 单位换算，毫米*2.835=Point单位
            e.Graphics.DrawString(str_express, font_express, bru_express, r_express, fmt_express);

        }



        private void button1_Click_1(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            string myff = str.ToString();
            //string myff = printDocument1.PrinterSettings.PrinterName;
            MessageBox.Show(myff);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            //Class1.serverSocket.Close();
            //MessageBox.Show("关闭了");
            //Environment.Exit(0);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            string ps = textBox2.Text;
            if (ps != "")
            {
                Form1.fontsize = Convert.ToInt32(ps);
            }
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string ppd = textBox1.Text;
            ppd = ppd.Trim();
            this.print_str = ppd;
        }


    }
}
